package com.example.demo.dao;


import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * 基础类，主要对MyBatis的方法进行封装
 * @author Henry.Y
 * 2019年5月15日
 */
public class BaseDao {
	
	private int temp;
	
	private SqlSession session;
	
	protected Set<String> roleSet= new HashSet<String>();
	
	protected Set<String> permissionSet= new HashSet<String>();
	
	@SuppressWarnings("rawtypes")
	private List list;

	/**
	 * 获取SqlSession对象
	 * @return session
	 */
	private SqlSession getSession(){
		InputStream input=null;
		try{
			input= this.getClass().getClassLoader().getResourceAsStream("mybatis/MyBatis-Configuration.xml");                                                            
			//System.out.println(this.getClass().getClassLoader().getResource("mybatis/MyBatis-Configuration.xml").getPath());		
		}catch(Exception e){
			
			e.printStackTrace();
		}
	    return new SqlSessionFactoryBuilder().build(input).openSession();
	}
	
	/**
	 * 插入语句
	 * @param sqlId 插入的sql语句
	 * @param object 插入的对象
	 * @return
	 */
	protected int operateInsertSql(String sqlId,Object object){
		session= getSession();
		temp= session.insert(sqlId,object);
		session.commit();
		session.close();
		return temp;
	}
	
	/**
	 * 删除语句
	 * @param sqlId 删除的sql语句
	 * @param object 删除的对象
	 * @return
	 */
	protected int operateDeleteSql(String sqlId,Object object){
		session= getSession();
	    temp= session.delete(sqlId,object);			
		session.commit();
		session.close();
		return temp;
	}
	
	/**
	 * 修改语句
	 * @param sqlId 修改的sql语句
	 * @param object 修改的对象
	 * @return
	 */
	protected int operateUpdateSql(String sqlId,Object object){
		session= getSession();
		temp= session.update(sqlId,object);
		session.commit();
		session.close();
		return temp;
	}
	
	/**
	 * 查询单个结果语句
	 * @param sqlId 修改的sql语句
	 * @param object 查询的条件
	 * @return
	 */
	protected Object operateSelectOneSql(String sqlId,Object object){
		session= getSession();
		Object disObject= getSession().selectOne(sqlId,object);
		session.commit();
		session.close();
		return disObject;
	}
	
	/**
	 * 查询语句
	 * @param sqlId 修改的sql语句
	 * @return 
	 */
	@SuppressWarnings("rawtypes")
	protected List operateSelectSql(String sqlId){
		session= getSession();
		list= getSession().selectList(sqlId);
		session.commit();
		session.close();
		return list;
	}
	
	/**
	 * 查询语句
	 * @param sqlId 修改的sql语句
	 * @param object 查询参数
	 * @return 
	 */
	@SuppressWarnings("rawtypes")
	protected List operateSelectSql(String sqlId,Object object){
		session= getSession();
		list= session.selectList(sqlId,object);
		session.commit();
		session.close();
		return list;
	}
	
	/**
	 * 全表删除语句
	 * @param sqlId 删除的sql
	 * @return
	 */
	protected int deleteForFullTable(String sqlId){
		session= getSession();
		temp= session.delete(sqlId);
		session.commit();
		session.close();
		return temp;
	}
}
